<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Python的列表、集合与字典解析式 | 木偶&#39;s Blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon_64.ico">
    <meta name="description" content="技术人生：记录科技与生活的点点滴滴">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.917e5670.css" as="style"><link rel="preload" href="/assets/js/app.debacc6b.js" as="script"><link rel="preload" href="/assets/js/3.5255d809.js" as="script"><link rel="preload" href="/assets/js/1.2ffa0b83.js" as="script"><link rel="preload" href="/assets/js/11.5ab14d74.js" as="script"><link rel="prefetch" href="/assets/js/10.20f41999.js"><link rel="prefetch" href="/assets/js/12.ad9be551.js"><link rel="prefetch" href="/assets/js/13.b27ec5a7.js"><link rel="prefetch" href="/assets/js/14.81da2467.js"><link rel="prefetch" href="/assets/js/15.ef12d201.js"><link rel="prefetch" href="/assets/js/16.a1333da5.js"><link rel="prefetch" href="/assets/js/17.2fb2d104.js"><link rel="prefetch" href="/assets/js/18.ccbeeef9.js"><link rel="prefetch" href="/assets/js/19.b45c5ebe.js"><link rel="prefetch" href="/assets/js/20.2483a194.js"><link rel="prefetch" href="/assets/js/21.3024fbc5.js"><link rel="prefetch" href="/assets/js/22.667c1ce8.js"><link rel="prefetch" href="/assets/js/23.db8a78b4.js"><link rel="prefetch" href="/assets/js/24.ba99df67.js"><link rel="prefetch" href="/assets/js/25.cf8a1716.js"><link rel="prefetch" href="/assets/js/26.9e00c93e.js"><link rel="prefetch" href="/assets/js/27.20b52c8f.js"><link rel="prefetch" href="/assets/js/28.84978b7f.js"><link rel="prefetch" href="/assets/js/29.a2830380.js"><link rel="prefetch" href="/assets/js/30.93fff66a.js"><link rel="prefetch" href="/assets/js/31.14a156ef.js"><link rel="prefetch" href="/assets/js/32.2f875164.js"><link rel="prefetch" href="/assets/js/33.784ba9c4.js"><link rel="prefetch" href="/assets/js/34.a534ca0a.js"><link rel="prefetch" href="/assets/js/35.d4210d5f.js"><link rel="prefetch" href="/assets/js/36.a768fc8b.js"><link rel="prefetch" href="/assets/js/37.33b83c4c.js"><link rel="prefetch" href="/assets/js/38.64aed905.js"><link rel="prefetch" href="/assets/js/39.e47582a6.js"><link rel="prefetch" href="/assets/js/4.cf73976a.js"><link rel="prefetch" href="/assets/js/40.fd46c14e.js"><link rel="prefetch" href="/assets/js/41.e2bba590.js"><link rel="prefetch" href="/assets/js/42.f51d9b0d.js"><link rel="prefetch" href="/assets/js/43.547263e1.js"><link rel="prefetch" href="/assets/js/5.7a9baacf.js"><link rel="prefetch" href="/assets/js/6.1064904d.js"><link rel="prefetch" href="/assets/js/7.baf14459.js"><link rel="prefetch" href="/assets/js/8.b7d110a6.js"><link rel="prefetch" href="/assets/js/9.73858664.js">
    <link rel="stylesheet" href="/assets/css/0.styles.917e5670.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>木偶's Blog</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>技术人生：记录科技与生活的点点滴滴</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>puppet</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="木偶's Blog" class="logo"> <span class="site-name">木偶's Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      IT笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/Python/" class="nav-link"><i class="undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      关于我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/puppetdevz" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    puppet
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>9</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>1</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      IT笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/Python/" class="nav-link"><i class="undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      关于我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/puppetdevz" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Python的列表、集合与字典解析式</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>puppet</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">Python的列表、集合与字典解析式</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>puppet</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2022/2/18</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Python</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="python的列表、集合与字典解析式"><a href="#python的列表、集合与字典解析式" class="header-anchor">#</a> Python的列表、集合与字典解析式</h1> <p>这三种都是 Python 的语法糖。</p> <p>所谓“语法糖”，个人理解是将一些底层或基本的操作封装起来，使其易用、高效。</p> <h2 id="列表解析式"><a href="#列表解析式" class="header-anchor">#</a> 列表解析式</h2> <h3 id="常规-expr-for-i-in-iter"><a href="#常规-expr-for-i-in-iter" class="header-anchor">#</a> 常规：[expr for i in iter]</h3> <p>举例：要求 1-10 的每个数字的算术平方根组成的列表。</p> <p>用 for 循环：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 不用 pow() 是因为这样计算性能更好</span>
    i <span class="token operator">=</span> i <span class="token operator">**</span> <span class="token number">0.5</span> 
    result<span class="token punctuation">.</span>append<span class="token punctuation">(</span>i<span class="token punctuation">)</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>而用“列表解析式”</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>result <span class="token operator">=</span> <span class="token punctuation">[</span> i <span class="token operator">**</span> <span class="token number">0.5</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="进阶-expr-for-i-in-iter-if-for-j-in-iter-if"><a href="#进阶-expr-for-i-in-iter-if-for-j-in-iter-if" class="header-anchor">#</a> 进阶：[expr for i in iter if… for j in iter if…]</h3> <p><code>[expr for i in iter if… for j in iter if…]</code> 就相当于：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">iter</span><span class="token punctuation">:</span>
	<span class="token keyword">if</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">:</span>
    	<span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">iter</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">:</span>
                result<span class="token punctuation">.</span>append<span class="token punctuation">(</span>expr<span class="token punctuation">(</span>i<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>举例：取 10 以内的单数和 10 以内的双数组成的所有二元组，把这些元组放在一个列表里</p> <p>如果用 for 循环：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> i <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
        <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> j <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
                result<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span>j<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>列表解析式：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token punctuation">[</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> j<span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">if</span> i <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">!=</span> <span class="token number">0</span> <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token keyword">not</span> j <span class="token operator">%</span> <span class="token number">2</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="sum-up"><a href="#sum-up" class="header-anchor">#</a> Sum Up</h3> <ul><li><p>列表解析式里不能有 elif 等比较复杂的语法结构，如果实在不行，则只能用 for 循环了。</p></li> <li><p>列表解析式只会将 <code>[expr for i in iter]</code> 中的 expr 的返回值放入列表中</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token punctuation">[</span><span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token comment"># 该列表解析式最终生成的是 [None, None, None, None, None]，因为 print() 没有返回值</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul> <h2 id="集合解析式"><a href="#集合解析式" class="header-anchor">#</a> 集合解析式</h2> <p>用法与列表解析式基本一致，只是将 <code>[]</code> 替换为 <code>{}</code></p> <ul><li><code>{ expr for i in iter }</code></li> <li><code>{ expr for i in iter if… for j in iter if… }</code></li></ul> <p>但与“列表解析式”有几点不同：</p> <ul><li><p>集合里面的元素必须是“可哈希的”</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 比如不可以这样子，因为这样生成的元素都是 list，不可散列，所以会报错。</span>
<span class="token punctuation">{</span> <span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p>集合解析式会自动去重</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token punctuation">{</span> i <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token punctuation">}</span>
<span class="token comment"># 结果为：{ 1, }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul> <h2 id="字典解析式"><a href="#字典解析式" class="header-anchor">#</a> 字典解析式</h2> <p>用法与“集合解析式”、“列表解析式”基本一致。用的是 <code>{}</code>，同时 expr 必须是 key-value 形式</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token punctuation">{</span> i<span class="token punctuation">:</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token string">'abc'</span><span class="token punctuation">}</span>
<span class="token comment"># 输入结果：{ 0: ['c'], 1: ['c'], 2: ['c'] }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>字典的 key 是去重的，所以最后都只会剩下 value 为 <code>['c']</code> 的情况</li> <li>key 必须<strong>可散列</strong>的，但是 value 不需要<strong>可散列</strong>，所 以value 可以用可变的数据结构，例如 list</li></ul> <h2 id="参考链接"><a href="#参考链接" class="header-anchor">#</a> 参考链接</h2> <ul><li><a href="https://blog.csdn.net/LittleHuang950620/article/details/81774402" target="_blank" rel="noopener noreferrer">Python的列表解析式，集合解析式，字典解析式_LittleHuang950620的博客-CSDN博客_python 字典解析式<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/3/14 下午3:33:25</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/blogs/python/lie-biao-jie-xi-shi-ji-he-jie-xi-shi-he-zi-dian-jie-xi-shi.html#列表解析式" class="sidebar-link reco-side-列表解析式" data-v-cb1513f6>列表解析式</a></li><li class="level-3" data-v-cb1513f6><a href="/blogs/python/lie-biao-jie-xi-shi-ji-he-jie-xi-shi-he-zi-dian-jie-xi-shi.html#常规-expr-for-i-in-iter" class="sidebar-link reco-side-常规-expr-for-i-in-iter" data-v-cb1513f6>常规：[expr for i in iter]</a></li><li class="level-3" data-v-cb1513f6><a href="/blogs/python/lie-biao-jie-xi-shi-ji-he-jie-xi-shi-he-zi-dian-jie-xi-shi.html#进阶-expr-for-i-in-iter-if-for-j-in-iter-if" class="sidebar-link reco-side-进阶-expr-for-i-in-iter-if-for-j-in-iter-if" data-v-cb1513f6>进阶：[expr for i in iter if… for j in iter if…]</a></li><li class="level-3" data-v-cb1513f6><a href="/blogs/python/lie-biao-jie-xi-shi-ji-he-jie-xi-shi-he-zi-dian-jie-xi-shi.html#sum-up" class="sidebar-link reco-side-sum-up" data-v-cb1513f6>Sum Up</a></li><li class="level-2" data-v-cb1513f6><a href="/blogs/python/lie-biao-jie-xi-shi-ji-he-jie-xi-shi-he-zi-dian-jie-xi-shi.html#集合解析式" class="sidebar-link reco-side-集合解析式" data-v-cb1513f6>集合解析式</a></li><li class="level-2" data-v-cb1513f6><a href="/blogs/python/lie-biao-jie-xi-shi-ji-he-jie-xi-shi-he-zi-dian-jie-xi-shi.html#字典解析式" class="sidebar-link reco-side-字典解析式" data-v-cb1513f6>字典解析式</a></li><li class="level-2" data-v-cb1513f6><a href="/blogs/python/lie-biao-jie-xi-shi-ji-he-jie-xi-shi-he-zi-dian-jie-xi-shi.html#参考链接" class="sidebar-link reco-side-参考链接" data-v-cb1513f6>参考链接</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.debacc6b.js" defer></script><script src="/assets/js/3.5255d809.js" defer></script><script src="/assets/js/1.2ffa0b83.js" defer></script><script src="/assets/js/11.5ab14d74.js" defer></script>
  </body>
</html>
